﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;
using OSA.Policlinic.UI.WFApplicationWCF.RegistryServiceReference;

namespace OSA.Policlinic.UI.WFApplication
{
    public partial class ShowDoctorView : UserControl
    {
        private readonly IRegistryBusinessApplication registryBusinessApplication;
        private List<Doctor> doctors;
        private mainForm owner;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public ShowDoctorView(mainForm form)
        {
            InitializeComponent();
            doctors=new List<Doctor>();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            doctors.AddRange(registryBusinessApplication.GetAllDoctors());
            doctorBindingSource.DataSource = doctors;
            owner = form;
            logger.Trace("Контрол ShowDoctorView успешно инициализирован");
        }

        private void SearchBySpecializationButton_Click(object sender, EventArgs e)
        {
            if (SpecializationTextBox.Text.Trim().Length != 0)
            {
                doctors.AddRange(registryBusinessApplication.GetDoctorsBySpecialisation(SpecializationTextBox.Text));
                doctorBindingSource.DataSource = doctors;
                logger.Debug("Произведен поиск врачей специализации {0}", SpecializationTextBox.Text);
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            doctors.AddRange(registryBusinessApplication.GetAllDoctors());
            doctorBindingSource.DataSource = doctors;
        }

        private void SelectDoctorButton_Click(object sender, EventArgs e)
        {
            if (owner.mainPanel.Controls.Count > 0)
            {
                owner.mainPanel.Controls.Clear();
            }
            Doctor doctor = (Doctor)DoctorsDataGridView.CurrentRow.DataBoundItem;
            if (owner.ticketControl == null)
            {
                owner.ticketControl = new TicketControl(doctor);
                owner.ticketControl.Dock = DockStyle.Fill;

            }            
            else
            {
                owner.ticketControl.doctor = doctor;
                owner.ticketControl.FillDoctor();
                owner.ticketControl.ClearPatient();
                owner.ticketControl.ClearService();
            }
            owner.mainPanel.Controls.Add(owner.ticketControl);
            owner.label1.Text = "Выдать новый талон";
            logger.Debug("Нажата кнопка 'Выдать талон' контрола ShowDoctorView");
        }

        private void DoctorsDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Doctor doctor = (Doctor)DoctorsDataGridView.CurrentRow.DataBoundItem;
            if(doctor != null)
            {
                TicketView ticketView = new TicketView(doctor);
                logger.Debug("Врач id = {0} выбран для просмотра существующих талонов", doctor.Id);
                ticketView.ShowDialog();
            }
        }    
    }
}
